package com.skzh.web.controller.drawtask;

import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.core.page.TableDataInfo;
import com.skzh.drawtask.domain.TaskLine;
import com.skzh.drawtask.domain.TaskLinePoints;
import com.skzh.drawtask.service.ILineService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author zr
 * @Description 线任务控制器
 * @Date 2025/3/31 13:31
 */
@RestController
@RequestMapping("/lineTask/lineInfo")
public class Line extends BaseController {

    @Autowired
    private ILineService lineService;

    /**
     * 新增线任务
     */
    @ApiOperation(value = "新增线任务")
    @PostMapping
    public AjaxResult add(@RequestBody TaskLine taskLine) {
        return toAjax(lineService.insertTaskLine(taskLine));
    }

    /**
     * 根据ID删除线任务
     */
    @ApiOperation(value = "根据ID删除线任务")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(lineService.deleteTaskLineById(id));
    }


    /**
     * 查询线任务
     */
    @ApiOperation(value = "查询线任务")
    @GetMapping("/list")
    public TableDataInfo list(TaskLine taskLine) {
            //查询线主体
            List<TaskLine> taskLines = lineService.selectTaskLineList(taskLine);
            //取出taskLines所有元素的id
            List<Long> lineIds = taskLines.stream().map(TaskLine::getId).collect(Collectors.toList());
            //根据ID批量查询对应线的顶点信息
            List<TaskLinePoints> linePointsList = lineService.selectTaskLinePointsByIds(lineIds);

            //创建Map来存储每个线ID对应的顶点信息
            Map<Long, List<TaskLinePoints>> pointsMap = linePointsList.stream()
                    .collect(Collectors.groupingBy(TaskLinePoints::getLineId));

            //创建列表存储组合后的线及其顶点信息
            List<TaskLine> lineWithPointsList = new ArrayList<>();
            for (TaskLine line : taskLines) {
                TaskLine lineAndPoint = new TaskLine();
                List<TaskLinePoints> taskLinePoints = pointsMap.getOrDefault(line.getId(), Collections.emptyList());
                lineAndPoint.setId(line.getId());
                lineAndPoint.setLineName(line.getLineName());
                lineAndPoint.setLineColor(line.getLineColor());
                lineAndPoint.setLineLength(line.getLineLength());
                lineAndPoint.setPoints(taskLinePoints.stream()
                        .map(point -> {
                            TaskLinePoints linePoints = new TaskLinePoints();
                            linePoints.setXLongitude(point.getXLongitude());
                            linePoints.setXLatitude(point.getXLatitude());
                            linePoints.setXOrder(point.getXOrder());
                            return linePoints;
                        })
                        .collect(Collectors.toList()));
                lineWithPointsList.add(lineAndPoint);
            }

            return getDataTable(lineWithPointsList);

    }


}
